package com.ttl.bc.ic.stktransfer;

import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ttl.bc.ic.stktransfer2.BCStkTransfSub2;
import com.ttl.bc.ic.stktransfer2.BCStkTransfer2;
import com.ttl.bc.processstock.bean.ProcessStockBean;
import com.ttl.common.db.ADAO;

public class BCStkTransferDAO extends ADAO {

	@SuppressWarnings("unchecked")
	public boolean add(BCStkTransfer s, ArrayList<BCStkTransfSub3> s3) {
		Session session = null;

		
		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            String hql = "FROM BCStkTransfer where docNo = '"+s.getDocNo()+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){
            	session.delete(resultList.get(i));
            }            
            
            hql = "FROM BCStkTransfSub3 where docNo = '"+s.getDocNo()+"'";
            q = session.createQuery(hql);
            resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){         
                session.delete(resultList.get(i));
            }            
            
            hql = "FROM BCStkTransfer2 where docNo = '"+s.getDocNo()+"'";
            q = session.createQuery(hql);
            resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){
            	BCStkTransfer2 s2 = (BCStkTransfer2)resultList.get(i);
            	s2.setIsConfirm(1);
            	
            	double remainQty = 0;
            	Iterator it2 = s2.getBcStkTransfSubs().iterator();
            	while (it2.hasNext()) {
					BCStkTransfSub2 sub2 = (BCStkTransfSub2) it2.next();
					
	            	Iterator it = s.getBcStkTransfSubs().iterator();
	            	while (it.hasNext()) {
						BCStkTransfSub sub = (BCStkTransfSub) it.next();
						if(sub2.getItemCode().equals(sub.getItemCode())) {
//ลบ remain 					sub2.setRemainQty(sub2.getRemainQty()-sub.getQty());	

							sub2.setQty(sub.getQty());
							sub2.setRemainQty(0d);
						}
					}	
	            	remainQty += sub2.getRemainQty();
				}
            	
            	if(remainQty==0) {
            		s2.setBillStatus(2);
            	}else {
            		s2.setBillStatus(1);
            	}
            	
            	session.save(s2);
            }
            
			session.save(s);
			for (int i = 0; i < s3.size(); i++) {
				session.save(s3.get(i));	
			}
			
			
        	Iterator it = s.getBcStkTransfSubs().iterator();
        	while (it.hasNext()) {
				BCStkTransfSub sub = (BCStkTransfSub) it.next();
				
				ProcessStockBean p = new ProcessStockBean();
				p.setItemCode(sub.getItemCode());
				p.setDocDate(sub.getDocDate());
				p.setProcessFlag(0);
				p.setFlowStatus(1);
				p.setProcessType(17);
				p.setProcessCase(1);
				session.save(p);
			}
			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();
			return false;
		}finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public boolean delete(BCStkTransfer s) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            String hql = "FROM BCStkTransfer where docNo = '"+s.getDocNo()+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){
            	session.delete(resultList.get(i));
            }            
            
            hql = "FROM BCStkTransfSub3 where docNo = '"+s.getDocNo()+"'";
            q = session.createQuery(hql);
            resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){         
                session.delete(resultList.get(i));
            }            
            
            hql = "FROM BCStkTransfer2 where docNo = '"+s.getDocNo()+"'";
            q = session.createQuery(hql);
            resultList = q.list();			
            
            for(int i=0; i<resultList.size(); i++){
            	BCStkTransfer2 s2 = (BCStkTransfer2)resultList.get(i);
            	s2.setIsConfirm(0);
            	s2.setBillStatus(0);
            	
            	Iterator it2 = s2.getBcStkTransfSubs().iterator();
            	while (it2.hasNext()) {
					BCStkTransfSub2 sub2 = (BCStkTransfSub2) it2.next();
					sub2.setRemainQty(sub2.getQty());	
				}
      
            	session.save(s2);
            }
            			
        	Iterator it = s.getBcStkTransfSubs().iterator();
        	while (it.hasNext()) {
				BCStkTransfSub sub = (BCStkTransfSub) it.next();
				
				ProcessStockBean p = new ProcessStockBean();
				p.setItemCode(sub.getItemCode());
				p.setDocDate(sub.getDocDate());
				p.setProcessFlag(0);
				p.setFlowStatus(1);
				p.setProcessType(17);
				p.setProcessCase(3);
				session.save(p);
			}
			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();
			return false;
		}finally {
			session.close();
		}
	}
}
